package com.example.springstatemachine.controller;

import com.example.springstatemachine.cache.TaskCache;
import com.example.springstatemachine.model.Task;
import com.example.springstatemachine.model.Transactions;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/tasks")
public class TaskController {

    @RequestMapping(value = "/getTasks")
    public ResponseEntity<List<Task>> getTasks() {
        List<Task> taskList = TaskCache.getTaskList();
        System.out.println(taskList);
        return ResponseEntity.ok(taskList);
    }

    @RequestMapping(value = "/initiateTask")
    public String initiateTask() {
        Task ky_task = new Task("1", "科员申请");
        Task kz_task = new Task("2", "科长审批");
        Task bgs_task = new Task("3", "办公室审批");
        Transactions ky_to_kz = new Transactions("1","送科长审批");
        Transactions ky_to_bgs = new Transactions("2","送办公室审批");
        Transactions kz_to_bgs = new Transactions("3","送办公室审批");
        ky_task.setToTransaction(ky_to_kz);
        ky_task.setToTransaction(ky_to_bgs);
        kz_task.setFromTransaction(ky_to_kz);
        kz_task.setToTransaction(kz_to_bgs);
        bgs_task.setFromTransaction(ky_to_bgs);
        bgs_task.setFromTransaction(kz_to_bgs);
        TaskCache.addTask(ky_task);
        TaskCache.addTask(kz_task);
        TaskCache.addTask(bgs_task);
        return "Initiate a new task";
    }
}
